#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import pickle
import shelve
  
PATH = "../data/morph/"

def store_morph_data(file):
  with open(file,'rb') as pickle_file:
    p = pickle.Unpickler(pickle_file)
    lemmas = p.load()
    rules = p.load()
    gramtab = p.load()
    prefixes = p.load()
    possible_rule_prefixes = p.load()
    endings = p.load()
    normal_forms = p.load()
    rule_freq = p.load or {}

  lemmas_shelve = shelve.open(PATH + 'lemmas.shv')
  for key in lemmas:
    lemmas_shelve[key.encode('utf-8')] = lemmas[key]

  rules_shelve = shelve.open(PATH + 'rules.shv')
  for key in rules:
    rules_shelve[str(key)] = rules[key]

  gramtab_shelve = shelve.open(PATH + 'gramtab.shv')
  for key in gramtab:
    gramtab_shelve[key.encode('utf-8')] = gramtab[key]

  prefixes_file = open(PATH + 'prefixes.pkl', 'wb')
  pickle.dump(prefixes, prefixes_file)

  possible_rule_prefixes_file = open(PATH + 'possible_rule_prefixes.pkl', 'wb')
  pickle.dump(possible_rule_prefixes, possible_rule_prefixes_file)

  endings_shelve = shelve.open(PATH + 'endings.shv')
  for key in endings:
    endings_shelve[key.encode('utf-8')] = endings[key]

  normal_forms_shelve = shelve.open(PATH + 'normal_forms.shv')
  for key in normal_forms:
    normal_forms_shelve[str(key)] = normal_forms[key]

store_morph_data(PATH + 'all.pickle')
